package com.hb.gp.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.hb.gp.GPInsCommand;
import com.hb.gp.GPType;
import com.hb.gp.IGPCommandMessageService;
import com.hb.gp.util.tools.DES;
import com.hb.gp.util.tools.GP_GetMac;

@GPType(INS = GPInsCommand.INS_52)
public class GP52 implements IGPCommandMessageService {

	@Override
	public String makeApduMap(String apdu, Map<String, Object> map)
			throws Exception {
		// TODO Auto-generated method stub
		String response = map.get(VARIBLE_RESPONSE).toString();
		// 圈存金额
		int blance = Integer.parseInt(String.valueOf(map.get(VARIBLE_BLANCE)));
		String factor = response.substring(16, 24) + response.substring(8, 12)
				+ "8000";
		String key = "0F89FE503170119AF5BCA7B9599F2C18";

		// System.out.println("factor:" + factor);
		String macKey = DES.encryptTDES_ECB(key, factor);
		// System.out.println("mackey:" + macKey);
		String date = getDateTime();
		String macIn = String.format("%08x", blance) + "02" + "999999999999"
				+ date;
		String mac = GP_GetMac.get4ByteIMac(macIn, macKey, "0000000000000000");
		// apdu = "805200000b20080808080808" + mac;
		apdu = "805200000b" + date + mac;
		return apdu;
	}

	@Override
	public String makeApduMap(String apdu, Map<String, Object> varibleMap,
			Map<String, Object> storeMap) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * yyyyMMddHHmmss
	 * 
	 * @return
	 */
	private String getDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(new Date());
	}
}
